package cn.hp.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.hp.domain.Student;

@Controller		//跳转页面，跳转到html页面
public class HelloController {

	@RequestMapping("/hello")
	public String hello() {
		return "hello";
	}
	
	@RequestMapping("/showStudent")
	public String showStudent() {
		return "showStudent";
	}
	
	//把数据存储到Model里面
	//读取Model里面的对象
	@RequestMapping("/getOneStudent")
	public String getOneStudent(Model model) {
		Student student = new Student(1,"张三","河南郑州","男",new Date(),898.123);
		model.addAttribute("stu", student);
		return "getOneStudent";
	}
	
	//把数据存储到Model里面
	//读取Model里面的集合
	@RequestMapping("/getAllStudent")
	public String getAllStudent(Model model) {
		List<Student> studentList = new ArrayList<>();
		for (int i = 1; i <4; i++) {
			Student student = new Student(i,"张三","河南郑州","男",new Date(),898.123);
			studentList.add(student);
		}
		model.addAttribute("studentList", studentList);
		return "getAllStudent";
	}
	
	/*@Autowired
	private HttpServletRquest request;*/
	
	//从request，session,servletContext中获取数据
	@RequestMapping("getObject")
	public String getObject(HttpServletRequest request) {
		request.setAttribute("req-name", "req-小明");
		HttpSession session = request.getSession();
		session.setAttribute("ses-name", "ses-小明");
		//servletContext:上下文对象，域对象，代表整个应用
		ServletContext servletContext = request.getServletContext();
		servletContext.setAttribute("ser-name", "ser-小明");
		return "getObject";
	}
}
